package com.hazy.animalface.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hazy.animalface.R;
import com.hazy.animalface.module.StickerBag;
import com.hazy.animalface.view.StickersActivity;

import java.util.List;

/**
 * Created by pengjiahui on 2017/4/15 0015.
 * <p/>
 * 服务器头贴的适配器
 */
public class UploadStickersAdapter extends RecyclerView.Adapter<UploadStickersAdapter.MyViewHolder> {

    private Context context;
    private List<StickerBag> stickerBags;

    public UploadStickersAdapter(Context context, List<StickerBag> stickerBags) {
        this.context = context;
        this.stickerBags = stickerBags;
    }

    @Override
    public UploadStickersAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_sticker_server_4_manager, parent, false));
    }

    @Override
    public void onBindViewHolder(final UploadStickersAdapter.MyViewHolder holder, int position) {
        final StickerBag stickerBag = stickerBags.get(position);
        if (stickerBag != null) {
            holder.mThumbImageView.setImageURI(stickerBag.getThumb().getThumbnailUrl(true, 100, 100));
            if (stickerBag.getName() != null) {
                holder.mNameTextView.setText(stickerBag.getName());
            }
            if (stickerBag.getPrice() != null) {
                Double price = Double.valueOf(stickerBag.getPrice());
                if (price > 0) {
                    holder.mPriceTextView.setText("￥ " + stickerBag.getPrice());
                } else {
                    holder.mPriceTextView.setText("免 费");
                }
            } else {
                holder.mPriceTextView.setText("免 费");
            }
            holder.item.setOnClickListener(new StickerItemClickListener(stickerBag));
//            holder.item.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    EventBus.getDefault().post(new DeleteStickerBagEvent(stickerBag));
//                    return false;
//                }
//            });
        }
    }

    private class StickerItemClickListener implements View.OnClickListener {

        private StickerBag stickerBag;

        public StickerItemClickListener(StickerBag stickerBag) {
            this.stickerBag = stickerBag;
        }

        @Override
        public void onClick(View view) {
            StickersActivity.actionStart(context, stickerBag, false);
        }
    }

    @Override
    public int getItemCount() {
        return stickerBags == null ? 0 : stickerBags.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        View item;
        SimpleDraweeView mThumbImageView;
        TextView mPriceTextView, mNameTextView;

        public MyViewHolder(View itemView) {
            super(itemView);
            item = itemView;
            mThumbImageView = (SimpleDraweeView) itemView.findViewById(R.id.item_sticker_iv);
            mNameTextView = (TextView) itemView.findViewById(R.id.item_stickers_name_tv);
            mPriceTextView = (TextView) itemView.findViewById(R.id.item_stickers_price_tv);
        }
    }
}
